package com.liuyk.commonlib.update;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;

import com.liuyk.commonlib.help.UpdateHelp;
import com.liuyk.commonlib.utility.CommonIntentUtils;

import java.io.File;

public class DownBroadcastReceiver extends BroadcastReceiver {
    //点击重试
    public static final String RETRY_ACTION = "download_retry";
    //点击安装
    public static final String INSTALL_ACTION = "install_apk";

    @Override
    public void onReceive(Context context, Intent i) {
        String action = i.getAction();
        String versionName = i.getStringExtra("versionName");
        String apkUrl = i.getStringExtra("apkUrl");

        if (RETRY_ACTION.equals(action)) {
            final Intent upIntent = new Intent(context, UpdateServer.class);
            upIntent.putExtra("apkUrl", apkUrl);
            upIntent.putExtra("versionName", versionName);
            context.startService(upIntent);
        }
        //安装
        if (INSTALL_ACTION.equals(action)) {
            UpdateNotifyManager.getInstance(context.getApplicationContext()).cancel();
            File apkCacheFile = UpdateHelp.getApkCacheFile(context, versionName);
            if (apkCacheFile != null && apkCacheFile.exists()) {
                CommonIntentUtils.goToInstallApk(context.getApplicationContext(), Uri.fromFile(apkCacheFile));
            }
        }
    }
}